package com.hkd.forum.controller;

import com.hkd.forum.common.AppResult;
import com.hkd.forum.common.ResultCode;
import com.hkd.forum.config.AppConfig;
import com.hkd.forum.model.Article;
import com.hkd.forum.model.ArticleReply;
import com.hkd.forum.model.User;
import com.hkd.forum.service.IArticleReplyService;
import com.hkd.forum.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private IArticleReplyService articleReplyService;

    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam(value = "帖⼦Id")  @RequestParam(value = "articleId") @NonNull Long articleId,
                            @ApiParam(value = "回复内容") @RequestParam(value = "content") @NonNull String content){
        //获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //判断用户是否禁言
        if(user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        //获取要回复帖子信息
        Article article = articleService.selectById(articleId);
        //判断帖子双胎
        if(article == null ){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(article.getState() == 1 || article.getDeleteState() == 1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //构建恢复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setPostUserId(user.getId());
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);
        //写入恢复
        articleReplyService.create(articleReply);
        //返回
        return AppResult.success();
    }

    @ApiOperation("获取帖子的回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId (@ApiParam("帖子Id") @RequestParam("articleId") @NonNull Long articleId){
        //校验帖子是否存在
        Article article = articleService.selectById(articleId);
        if(article == null){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(article.getDeleteState() == 1 || article.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //获取帖子的回复列表
        List<ArticleReply> result = articleReplyService.selectByArticleId(articleId);
        //返回
        return AppResult.success(result);

    }
}
